// 初始化路由
import Vue from 'vue'
import VueRouter from 'vue-router'
import auth from '@/utils/auth'
// 导入组件
// 这个是普通导入的方式
// import Login from '@/views/login'
// import Layout from '@/views/Layout'
// import Welcome from '@/views/welcome'
// import Article from '@/views/article'
// import NotFound from '@/views/404'
// import Image from '@/views/image'
// import Publish from '@/views/publish'
// import Comment from '@/views/comment'
// import Fans from '@/views/fans'
// import Setting from '@/views/setting'

// 导入组件  这个
const  Login   = () => import( '@/views/login' )
const  Layout  = () => import( '@/views/Layout' )
const  Welcome = () => import( '@/views/welcome' )
const  Article = () => import( '@/views/article' )
const  NotFound= () => import( '@/views/404' )
const  Image   = () => import( '@/views/image' )
const  Publish = () => import( '@/views/publish' )
const  Comment = () => import( '@/views/comment' )
const  Fans    = () => import(/* webpackChunkName: "fans" */'@/views/fans' )
const  Setting = () => import( '@/views/setting' )
Vue.use(VueRouter)
const routes = [
    //登录
    { path: '/login', component: Login },
    // 首页架子
    {
        path: '/',
        component: Layout,
        children: [
            // 欢迎页面
            { path: '/', component: Welcome },
            // 内容管理
            { path: '/article', component: Article },
            // 素材管理
            { path: '/image', component: Image },
            // 发布文章
            { path: '/publish', component: Publish },
            // 评论管理
            { path: '/comment', component: Comment },
              // 粉丝管理
            { path: '/fans', component: Fans },
             // 个人设置
             { path: '/setting', component: Setting }
        ]

    },
    //通用  404
    { path: '*', component: NotFound }
]

const router = new VueRouter({
    routes
})
// 导航守卫（每次跳转路由的时候执行）
router.beforeEach((to, from, next) => {
    // to 即将跳转的路由对象
    // from 正在来开的路由对象
    // next() 下一步,放行  next('地址') 拦截，跳转去哪
    const user = auth.getUser()
        // 不是访问登录，且没有token信息，不能访问其他页面，拦截到登录
        if (to.path !== '/login' && !user.token) return next('/login')
        // 其他情况一概放行
    next()
})
//   导出到main.js中
export default router